// pages/user/index.js
const getTimeFn = require('../../utils/getNowTime.js')
import * as request from "../../utils/request/user/user"
console.log(request)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    greet: '',
    role: '',
    name: '',
    adminSet: {
      permissions: ["查看人员信息", "上传文件", "删除文件", "退出登录", "收藏", "添加用户", '修改密码'],
      icons: ["friends-o", 'edit', "delete-o", "revoke", "star-o", "add-o", 'setting-o'],
      type: ['userInfo', "upload", "delete", "outLogin", "star", "addUser", "set"],
    },
    userSet: {
      permissions: ["退出登录", "收藏", "修改密码"],
      icons: ["revoke", "star-o", "setting-o"],
      type: ["outLogin", "star", 'set'],
    },
    showModal: false,
    clickTarget: '',
    userPassword: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let user = wx.getStorageSync('userInfo')
    this.setData({
      name: user.data.userAccount,
      role: user.data.userRole
    })
    this.setData({
      greet: getTimeFn.getTime()
    })
  },
  handleClick(event) {
    const type = event.currentTarget.dataset.param
    console.log(type)
    if (type == "upload") {
      wx.navigateTo({
        url: '/pages/file/uploadFile',
        fail(err) {
          console.log(err)
        }
      })
    }
    if (type == "outLogin") {
      this.setData({
        clickTarget: type,
        showModal: true
      });
    }
    if (type == "addUser") {
      wx.navigateTo({
        url: '/pages/user/addUser',
        fail(err) {
          console.log(err)
        }
      })
    }
    if (type == "userInfo") {
      wx.navigateTo({
        url: '/pages/user/userInfo/index',
        fail(err) {
          console.log(err)
        }
      })
    }
    if (type == "star") {
      wx.navigateTo({
        url: '/pages/file/starFile/index',
      })
    }
    if (type == "delete") {
      wx.navigateTo({
        url: '/pages/file/deleteFile/index',
      })
    }
    /**
     * 修改密码
     * 取消
     */
    if (type == "set") {
      this.setData({
        clickTarget: type,
        showModal: true
      })
    }
  },
  onCancel() {
    this.setData({
      showModal: false
    });
  },
  async onConfirm() {
    let session = wx.getStorageSync('session')
    let res
    if (this.data.clickTarget == 'set') {
      let data = {}
      let info = wx.getStorageSync('userInfo')
      console.log(info)
      let {
        id,
        status,
        userRole,
        userAccount
      } = info.data
      data.id = id
      data.status = status
      data.userAccount = userAccount
      data.userRole = userRole
      data.userPassword = this.data.userPassword
      console.log(data)
      try {
        res = await request.updateUser(data, session)
        console.log(res)
        if (res.code === 0) {
          wx.showToast({
            title: '修改成功',
          })
          this.setData({
            showModal: false,
            clickTarget: '',
            userPassword: "",
          })
          wx.removeStorage({
            key: 'userInfo',
            success() {
              console.log("删除成功")
              console.log(wx.getStorageSync('session'))
            }
          })
          wx.removeStorage({
            key: 'session',
            success() {
              console.log("删除成功")
            }
          })
          wx.redirectTo({
            url: '/pages/login/index'
          });
        } else {
          wx.showToast({
            title: '修改失败',
            icon: "none",
          })
        }
        this.setData({
          userPassword: "",
        })

      } catch (err) {
        wx.showToast({
          title: '修改失败',
          icon: 'none'
        })
      }

      return
    }
    console.log(session)
    res = await request.logOut({}, session)
    console.log(res)
    if (res.code === 0) {
      wx.removeStorage({
        key: 'userInfo',
        success() {
          console.log("删除成功")
          console.log(wx.getStorageSync('session'))
        }
      })
      wx.removeStorage({
        key: 'session',
        success() {
          console.log("删除成功")
        }
      })
      this.setData({
        showModal: false,
        clickTarget: ''
      });
      wx.redirectTo({
        url: '/pages/login/index'
      });

    } else {
      wx.showToast({
        title: '退出失败',
        icon: 'none'
      });
    }

  },
  onInput(e) {
    this.setData({
      userPassword: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})